//
//  BiographyVc.m
//  Wy_Reading
//
//  Created by wangyang on 2020/2/10.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "BiographyVc.h"
#import "BiographyVcCell.h"
#import "DetailsVc.h"
#import "WyLnadViewController.h"
@interface BiographyVc ()<UICollectionViewDelegate,UICollectionViewDataSource,SLBannerViewDelegate>

@property (nonatomic,strong) UICollectionView      *WyCollectionView;
@property (nonatomic,strong)  SLBannerView         *WyHomeSLBannerView;
@property (nonatomic,strong)  NSMutableArray         *WyArray;

@end

@implementation BiographyVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WyWhiteColor;
    
    [self WyCollectionView];
    
//    if (@available(iOS 11.0, *)) {
//
//        self.WyCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//
//    }
}


- (NSMutableArray *)WyArray{
    
    
    if (!_WyArray) {
        _WyArray = [[NSMutableArray alloc] init];
        [_WyArray removeAllObjects];
        
         _WyArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Home" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil][@"人物传记"];
        
//        if (_type == 0) {
//
//        }
//
//       else if (_type == 1)
//       {
//            _WyArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Home" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil][@"人物传记"];
//
//       }
//        else if (_type == 2)
//        {
//             _WyArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Home" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil][@"名人墨迹"];
//
//        }
//        else if (_type == 3)
//        {
//             _WyArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Home" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil][@"新书频道"];
//
//        }
//
//        else  if (_type == 4) {
//             _WyArray = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Home" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil][@"好书排行"];
//        }
//
        
        
        NSLog(@"%@",_WyArray);
        
        
        
        
    }
    return _WyArray;
    
}

- (UICollectionView *)WyCollectionView

{
    if (!_WyCollectionView) {
        
        
        UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10; //设置每一行的间距
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(BiLiWidth(167), BiLiWidth(115));  //设置每个单元格的大小
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _WyCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _WyCollectionView.delegate=self;
        _WyCollectionView.dataSource=self;
        _WyCollectionView.backgroundColor = WyWhiteColor;
        _WyCollectionView.showsVerticalScrollIndicator=NO;
        [_WyCollectionView registerClass:[BiographyVcCell class] forCellWithReuseIdentifier:@"cell"];
        [_WyCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"View"];
        [self.view addSubview:_WyCollectionView];
        [_WyCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_offset(0);
        }];
        _WyCollectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Shuaxin)];
          
           
        [_WyCollectionView.mj_header beginRefreshing];
        
    }
    return _WyCollectionView;
}
- (void)Shuaxin{
    
    [self WyArray];
    [self.WyCollectionView reloadData];
    [self.WyCollectionView.mj_header endRefreshing];
    
}

#pragma mark --UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.WyArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *str = @"cell";
    BiographyVcCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    cell.PicImg.image = [UIImage imageNamed:self.WyArray[indexPath.row][@"img"]];
    cell.NameLabe.text = self.WyArray[indexPath.row][@"name"];
    cell.UserNameLabe.text = self.WyArray[indexPath.row][@"UserName"];
    cell.MoneyLabe.text = self.WyArray[indexPath.row][@"money"];
    cell.ScoreLabe.text = self.WyArray[indexPath.row][@"Score"];
    
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"View" forIndexPath:indexPath];
    if (kind == UICollectionElementKindSectionHeader) {
        view.backgroundColor = WyWhiteColor;
        
       
        NSMutableArray *  BannerViewArray = [NSMutableArray arrayWithObjects:@"home_banner1",@"home_banner2",@"home_banner3", nil];

        _WyHomeSLBannerView = [[SLBannerView alloc]init];
        _WyHomeSLBannerView.delegate = self;
        [view addSubview:_WyHomeSLBannerView];
        _WyHomeSLBannerView.durTimeInterval = 0.5;
        _WyHomeSLBannerView.layer.cornerRadius = 10;
        _WyHomeSLBannerView.clipsToBounds = YES;
        _WyHomeSLBannerView.imgStayTimeInterval = 3;
        _WyHomeSLBannerView.slImages = BannerViewArray;
        [_WyHomeSLBannerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(WySCREENWIDTH, BiLiWidth(253)));
            make.top.mas_offset(BiLiWidth(0));
            make.centerX.mas_offset(0);
        }];
        
        
        
        self.RecommendedLabe = [UILabel labelWithContent:@"每日推荐" superView:view textColor:WyBlackColor font:WySYSTEMFONT(15) textAlignment:NSTextAlignmentLeft];
        [self.RecommendedLabe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(22)));
            make.left.mas_offset(BiLiWidth(15));
            make.top.equalTo(self.WyHomeSLBannerView.bottom).offset(BiLiWidth(5));
        }];
        
        
       
       
        [view addSubview:self.BackView];
        self.BackView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        self.BackView.layer.shadowColor = [UIColor colorWithRed:8/255.0 green:6/255.0 blue:6/255.0 alpha:0.1].CGColor;
        self.BackView.layer.shadowOffset = CGSizeMake(0,2);
        self.BackView.layer.shadowOpacity = 1;
        self.BackView.layer.shadowRadius = 6;
        self.BackView.layer.cornerRadius = 10;
        self.BackView.userInteractionEnabled = YES;
        [self.BackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(WySCREENWIDTH-BiLiWidth(30), BiLiWidth(115)));
            make.top.equalTo(self.RecommendedLabe.bottom).offset(BiLiWidth(15));
            make.centerX.mas_offset(0);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapselector)];
        [self.BackView addGestureRecognizer:tap];
        
        
        
        self.PicImg = [UIImageView imageWithImgName:self.WyArray[indexPath.row][@"img"] superView:self.BackView];
        self.PicImg .backgroundColor = WyColor;
        WyViewRadius(self.PicImg , BiLiWidth(5));
        [self.PicImg  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(80), BiLiWidth(95)));
            make.top.mas_offset(BiLiWidth(10));
            make.left.mas_offset(BiLiWidth(20));
        }];
        
        
        
        
        NSArray *fuleiArray = @[@"傅雷家书",@"傅雷",@"综合评分：4.8",@"¥29.50"];
        for (int i = 0; i<fuleiArray.count; i++) {
            UILabel *labe = [UILabel labelWithContent:fuleiArray[i]superView:self.BackView textColor:WyBlackColor font:WySYSTEMFONT(14) textAlignment:NSTextAlignmentLeft];
            [labe mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(20)));
                make.left.equalTo(self.PicImg.right).offset(BiLiWidth(10));
                make.top.mas_offset(BiLiWidth(10)+BiLiWidth(25)*i);
            }];
            labe.tag = i;
            if (labe.tag == 0) {
                _NameLabe = labe;
            }else if (labe.tag == 1)
            {
                _UserNameLabe = labe;
                _UserNameLabe.textColor = UIColorFromRGB(0x999999);
                       
            }
            else if (labe.tag == 2)
            {
                _ScoreLabe = labe;
                _ScoreLabe.textColor = UIColorFromRGB(0xFADB14);
               
            }
            else if (labe.tag == 3)
            {
                _MoneyLabe = labe;
                _MoneyLabe.textColor = UIColorFromRGB(0xFF4949);
                
            }
        }
        
        
        
        
        self.HotBooksLabe = [UILabel labelWithContent:@"热销书籍" superView:view textColor:WyBlackColor font:WySYSTEMFONT(15) textAlignment:NSTextAlignmentLeft];
        [self.HotBooksLabe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(22)));
            make.left.mas_offset(BiLiWidth(15));
            make.top.equalTo(self.BackView.bottom).offset(BiLiWidth(15));
        }];
      
        
        
    }
    return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{
    
    return CGSizeMake(WySCREENWIDTH, BiLiWidth(448));
    
}
#pragma mark --UICollectionViewDeleGate
//点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                     //定时器
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

                       [MyProgressHUD dismiss];

           DetailsVc *Details = [[DetailsVc alloc]init];
              Details.DetailsVcDicy = self.WyArray[indexPath.row];
              [self.navigationController pushViewController:Details animated:YES];
                     });
}

-(void)tapselector
{
     [[AlertControllerCustom shareInstance] showAlert:@"请点击热销书籍！\n查看详情" viewController:self];
}


//返回这个UIcollectionView是否可以被选择
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}





- (UILabel *)RecommendedLabe

{
    if (!_RecommendedLabe) {
        
        _RecommendedLabe = [[UILabel alloc]init];
        
    }
    return _RecommendedLabe;
}

- (UILabel *)HotBooksLabe

{
    if (!_HotBooksLabe) {
        
        _HotBooksLabe = [[UILabel alloc]init];
        
    }
    return _HotBooksLabe;
}

- (UIView *)BackView

{
    if (!_BackView) {
        
        _BackView = [[UIView alloc]init];
    }
    return _BackView;
}

- (UIImageView *)PicImg

{
    if (!_PicImg) {
        _PicImg = [[UIImageView alloc]init];
    }
    return _PicImg;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
